<template>
  <div class="goods-list-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索商品和添加商品区域 -->
      <el-row>
        <el-col :span="10">
          <el-input
            v-model="queryInfo.query"
            clearable=""
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button
            type="primary"
            class="addGoodsBtn"
            @click="openAddGoodsDialog"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <el-table border stripe :data="goodsList.goods">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="95">
        </el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70"
        ></el-table-column>
        <el-table-column label="创建时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsList.total"
      >
      </el-pagination>

      <!-- 添加商品对话框 -->
      <AddGoodsDialog></AddGoodsDialog>
    </el-card>
  </div>
</template>

<script>
import AddGoodsDialog from '@/components/goods_list/AddGoodsDialog.vue'
export default {
  name: 'GoodsList',
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      this.goodsList = res.data
      this.$message.success('获取商品列表成功！')
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    deleteGoods(id) {
      this.$confirm('确定要删除这个商品吗?', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('goods/' + id)
          if (res.meta.status !== 200) {
            return this.$message.error('删除商品失败！')
          }
          this.$message.success('删除商品成功！')
          this.getGoodsList()
        })
        .catch((err) => err)
    },
    openAddGoodsDialog() {
      // this.$store.commit('goods/addGoodsDialogVisibleState', true)
      this.$router.push({
        path: '/home/goods/add'
      })
    },
    editGoods(id) {
      this.$router.push({
        path: '/home/goods/edit/',
        query: {
          id
        }
      })
    }
  },
  created() {
    this.getGoodsList()
  },
  components: { AddGoodsDialog }
}
</script>

<style lang="less" scope>
.addGoodsBtn {
  margin-left: 20px !important;
}
.el-table {
  min-width: 950px;
}
</style>
